<?php
/**
 * Created by PhpStorm.
 * User: ruma
 * Date: 2019/1/17
 * Time: 16:58
 */

namespace app\admin\controller;


use app\admin\model\Config;
use app\common\controller\AdminBaseController;
use app\Excepiton\ParameterException;

class Index extends AdminBaseController
{

    public function index(){
        //加载模板文件
        $this->match();
        return $this->fetch();
    }

    /**
     * 修改用户页面
     */
    public function changeAdmin(){
        $admin = session('admin_name');
        if (IS_POST){
            try{
                (new Config())->changeAdmin();
            }
            catch (ParameterException $e){
                $str = arrayToString($e->msg);
                $this->error("修改失败!".$str);
            }
            $this->success('修改成功.');
        }
        else{
            $this->assign('admin',$admin);
            return $this->fetch();
        }
    }


    /**
     * 修改密码页面
     */
    public function change_password(){
        if (IS_AJAX){
            try{
                (new Config())->changePassword();
            }
            catch (ParameterException $e){
                $str = arrayToString($e->msg);
                $this->error("密码修改失败!".$str);
            }
            $this->success('密码修改成功.');
        }
        else{
            return $this->fetch();
        }
    }



    /**
     * 退出后台
     */

    public function exitAdmin(){
        session('admin_name',null);
        return $this->redirect('/');
    }

}